
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:检查礼包领取状态（是否有礼包，是否可领取）
 *
 * @author jerryliu
 * @since 2015年11月4日 下午7:25:46
 */
public class CheckGiftBagTask {
    private ArrayList<String> mCmdList = new ArrayList<String>();

    public CheckGiftBagResponse request(String username) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();
        HttpBaseRequestPackage req = new HttpBaseRequestPackage();
        req.setUserName(username);
        CheckGiftBagResponse respObj = new CheckGiftBagResponse();
        ResponsePackage resp = new LoginResponsePackage();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_CHECK_GIFT_BAG_STATE);
        params.put("username", username);
        requestParams.add(params);
        req.setRequestParams(requestParams);
        mCmdList.add(ProtocolCmd.CMD_CHECK_GIFT_BAG_STATE);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (Exception e) {
        }
        return null;
    }

    class LoginResponsePackage extends HttpBaseResponsePackage<CheckGiftBagResponse> {

        @Override
        protected void handleResponse(CheckGiftBagResponse response, String responseJsonStr) {
            try {
                LogUtil.d("response", "CheckGiftBagTask:" + responseJsonStr);
                JSONArray responseArray = new JSONArray(responseJsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.optJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.optString("code");
                    String message = jsonResp.optString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);
                    if (jsonResp.optInt("code") != 1) {
                        continue;
                    }
                    if (jsonResp.isNull("data")) {
                        continue;
                    }
                    // 根据cmd解析每个请求
                    if (ProtocolCmd.CMD_CHECK_GIFT_BAG_STATE.equalsIgnoreCase(cmd)) {
                        JSONObject jsonResult = jsonResp.optJSONObject("data");
                        if (jsonResult != null) {
                            response.setGiftBagTotalCount(jsonResult.optInt("hasgift"));
                            response.setGiftBagAllowedCount(jsonResult.optInt("cangetgift"));
                        }
                    }
                    response.setOk(true);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    public static class CheckGiftBagResponse extends ResponseMessage {

        private static final long serialVersionUID = -4820735216365527465L;

        private int giftBagTotalCount;// 礼包总数

        private int giftBagAllowedCount;// 能领取的礼包数

        public int getGiftBagTotalCount() {
            return giftBagTotalCount;
        }

        public void setGiftBagTotalCount(int giftBagTotalCount) {
            this.giftBagTotalCount = giftBagTotalCount;
        }

        public int getGiftBagAllowedCount() {
            return giftBagAllowedCount;
        }

        public void setGiftBagAllowedCount(int giftBagAllowedCount) {
            this.giftBagAllowedCount = giftBagAllowedCount;
        }
    }
}
